﻿using IRepositoryBll;
using Microsoft.AspNetCore.Mvc;
using RepositoryEntity;
using RepositorySystem.Models;
using RepositorySystem.Models.RoleInfoView;
using RepositorySystem.Models.RoleInfoViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;


namespace RepositorySystem.Controllers
{
    public class RoleInfoController : Controller
    {
        private IRoleInfoBll _roleInfoBll;
        private IUserInfoBll _userInfoBll;
        private IMenuInfoBll _menuInfoBll;
        public RoleInfoController(IRoleInfoBll roleInfoBll, IUserInfoBll userInfoBll , IMenuInfoBll menuInfoBll)
        {
            _roleInfoBll = roleInfoBll;
            _userInfoBll = userInfoBll;
            _menuInfoBll = menuInfoBll;
        }

        public IActionResult RoleInfoListView()
        {
            return View();
        }
        /// <summary>
        /// 添加角色页面
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public IActionResult AddRoleInfoView(AddRoleInfoViewModels model)
        {
            return View();
        }
        /// <summary>
        /// 添加角色
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddRoleInfo(AddRoleInfoViewModels model)
        {
            CustomActionResult res = new CustomActionResult();
            if (model.RoleName == null)
            {
                res.Msg = "角色名称不能为空";
                return Json(res);
            }

            //判断数据库有没有相同账号
            else if (_roleInfoBll.IsHadRoleInfo(model.RoleName))
            {
                res.Msg = "当前添加角色已存在";
                return Json(res);
            }
            else
            {
                RoleInfo roleInfo = new RoleInfo()
                {
                    ID = Guid.NewGuid().ToString(),
                    RoleName = model.RoleName,
                    Description = model.Description,
                    CreateTime = DateTime.Now,
                    IsDelete = false
                };
                bool isSuccess = _roleInfoBll.Add(roleInfo);
                if (isSuccess)
                {
                    res.IsSuccess = true;
                    res.Msg = "添加成功";
                    return Json(res);

                }
                else
                {
                    return Json(res);
                }

            }


        }
        /// <summary>
        /// 删除角色
        /// </summary>
        [HttpPost]
        public IActionResult DeleteRoleInfo(string roleInfoId)
        {

            CustomActionResult res = new CustomActionResult();
            if (_roleInfoBll.SoftDeleteEntity(roleInfoId))
            {
                res.Status = 1;
                res.Msg = "成功";
                res.IsSuccess = true;
                return Json(res);
            }
            else
            {
                return Json(res);
            }
        }

        /// <summary>
        /// 获得角色集合
        /// </summary>
        /// <returns></returns>
        public IActionResult GetRoleInfoList(int page, int limit, string roleName)
        {
            int count;
            object roleInfo = _roleInfoBll.GetRoleInfoListByPage(page, limit, out count, roleName);

            return Json(new
            {
                code = 0,
                msg = "成功",
                count = count,
                data = roleInfo
            });
            

        }
        /// <summary>
        /// 修改角色页面
        /// </summary>
        /// <returns></returns>
        public IActionResult UpdateRoleInfoView(string roleInfoId)
        {
            RoleInfo roleInfo = _roleInfoBll.GetEntity(roleInfoId);
            if (roleInfoId != null)
            {
                ViewBag.ID = roleInfo.ID;
                ViewBag.RoleName = roleInfo.RoleName;
                ViewBag.Description = roleInfo.Description;
            }
            return View();
        }
        /// <summary>
        /// 更新修改页面
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult UpdateRoleInfo(UpdateRoleInfoModels model)
        {
            CustomActionResult res = new CustomActionResult();
            if (_roleInfoBll.UpdateRoleInfo(model.ID, model.RoleName, model.Description))
            {
                res.IsSuccess = true;
                res.Msg = "成功";
                res.Status = 1;
                return Json(res);
            }
            else
            {
                return Json(res);
            }

        }
        /// <summary>
        /// 绑定用户页面
        /// </summary>
        /// <returns></returns>
        public IActionResult BindUserInfoView()
        {
            return View();
        }
        /// <summary>
        /// 获取用户绑定数据方法
        /// </summary>
        /// <returns></returns>
        public IActionResult GetBindUserInfo(string roleInfoId)
        {
            CustomActionResult res = new CustomActionResult();
            //获取用户信息下拉数据
            object userInfoOptions = _userInfoBll.GetSelectOption();
            //获取当前用户已经绑定的ID
            List<string> userInfoIds = _roleInfoBll.GetBindUserIds(roleInfoId);
            res.Status = 1;
            res.IsSuccess = true;
            res.Data = new
            {
                UserInfoOptions = userInfoOptions,
                UserInfoIds = userInfoIds
            };
            return Json(res);
        }
        /// <summary>
        /// 绑定用户方法
        /// </summary>
        /// <param name="roleInfoId"></param>
        /// <param name="userInfoId"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult BindUserInfo(string roleInfoId, List<string> userInfoIds)
        {
            CustomActionResult res = new CustomActionResult();
            _roleInfoBll.BindUserInfo(roleInfoId, userInfoIds);
            res.Status = 1;
            res.IsSuccess = true;
            res.Msg = "绑定成功";
            return Json(res);
        }
        /// <summary>
        /// 绑定菜单视图页面
        /// </summary>
        /// <returns></returns>
        public IActionResult BindMenuInfoView()
        {
            return View();
        }
        /// <summary>
        /// 获取绑定菜单数据
        /// </summary>                                           
        /// <returns></returns>
        public IActionResult GetBindMenuInfo(string roleInfoId)
        {
            CustomActionResult res = new CustomActionResult();
            //获取菜单信息备选数据
            object menuInfoOptions = _menuInfoBll.GetSelectOption();
            //角色获取当前菜单已经绑定的ID
            List<string> menuInfoIds = _roleInfoBll.GetBindMenuIds(roleInfoId);
            res.Status = 1;
            res.IsSuccess = true;
            res.Data = new
            {
                MenuInfoOptions = menuInfoOptions,
                MenuInfoIds = menuInfoIds
            };
            return Json(res);
        }

        /// <summary>
        /// 绑定菜单方法
        /// </summary>
        /// <param name="roleInfoId"></param>
        /// <param name="menuInfoIds"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult BindMenuInfo(string roleInfoId, List<string> menuInfoIds)
        {
            CustomActionResult res = new CustomActionResult();
            _roleInfoBll.BindMenuIds(roleInfoId, menuInfoIds);
            res.Status = 1;
            res.IsSuccess = true;
            res.Msg = "绑定成功";
            return Json(res);
        }
    }
}
